# 打开文件
f = open('a.txt','w',encoding='utf-8')
# 写文件
f.write("好好学习\n")
f.write("天天向上")
# 关闭文件
f.close()

# 打开文件
f = open("a.txt","r",encoding="utf-8")
# 读文件
buf = f.read()
print(buf)
# 关闭文件
f.close()

# with open()打开文件的好处：不用自己书写关闭文件的代码，会自动进行关闭
with open("a.txt","a",encoding="utf-8") as f:
    f.write("\ngood good study,day day up")

# 按行读取内容，一次读取一行内容
with open("a.txt","r",encoding="utf-8") as f:
    buf = f.readline()
    print(buf)
    print(f.readline())

with open("a.txt",encoding="utf-8") as f:
    for line in f:
        print(line,end="")

with open("a.txt",encoding="utf-8") as f:
    while True:
        buf = f.readline()
        if len(buf) == 0:
            break
        else:
            print(buf,end="")